Imports System.Data.SqlClient

Friend Class DBNextIDValue
    Inherits DBBase

    Private msParameterName As String
    Private mlValue As Long = -1

    Public Sub New(ByVal sParameterName As String)
        msParameterName = sParameterName
    End Sub

    Public Function NextValue() As Long
        mlValue = -1

        Try
            Dim oCon As New SqlConnection(msConnectionString)
            oCon.Open()

            Dim oCMD As New SqlCommand("p_GetNextIDValue", oCon)
            oCMD.CommandType = CommandType.StoredProcedure

            oCMD.Parameters.Add("@ParameterName", SqlDbType.VarChar, 255)
            oCMD.Parameters(0).Value = msParameterName

            oCMD.Parameters.Add("@NextIDValue", SqlDbType.BigInt)
            oCMD.Parameters(1).Direction = ParameterDirection.Output

            oCMD.ExecuteNonQuery()

            mlValue = oCMD.Parameters(1).Value

            oCMD.Dispose()

            oCon.Close()
            oCon.Dispose()

            If mlValue = -1 Then
                Dim oEX As New InvalidIDException("Unable to obtain ID for " & msParameterName)
                Throw oEX
            End If

        Catch oEX As Exception
            Dim oLog As New ErrorLog.Log
            oLog.LogMessage(oEX)
        End Try

        Return mlValue
    End Function
End Class
